package com.example.yjh_app;

import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.lib_mvp.arouter.RouterUtils;
import com.example.lib_mvp.arouter.path.ArouterPath;
import com.example.lib_mvp.base.AppBaseActivity;
import com.example.lib_mvp.base.AppBaseFragment;
import com.example.lib_mvp.views.CustomDrawerLayout;
import com.tjf.lib_utils.ShareUtils;
import com.tjf.lib_utils.bean.BusEvent;

@Route(path = ArouterPath.PATH_APP_MAIN_ACTIVITY)
public class MainActivity extends AppBaseActivity {
    public FrameLayout fragmentLeftFly;
    public FrameLayout fragmentContainerFly;
    public ImageView mainSearchBgImg;
    public ImageView mainSearchImg;
    public RadioButton mainHomeRbt;
    public RadioButton mainInputRbt;
    public RadioButton mainSearchRbt;
    public RadioButton mainToolRbt;
    public RadioButton mainMeRbt;
    public RadioGroup mainListRgp;
    public CustomDrawerLayout drawerLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.hd_page_activity_main;
    }

    @Override
    protected void initView() {
        initViews();
    }

    @Override
    protected void initData() {
        initFragment();
    }

    private void initViews() {
        fragmentLeftFly = findViewById(R.id.fragment_left_fly);
        fragmentContainerFly = findViewById(R.id.fragment_container_fly);
        mainSearchBgImg = findViewById(R.id.main_search_bg_img);
        mainSearchImg = findViewById(R.id.main_search_img);
        mainHomeRbt = findViewById(R.id.main_home_rbt);
        mainInputRbt = findViewById(R.id.main_input_rbt);
        mainSearchRbt = findViewById(R.id.main_search_rbt);
        mainToolRbt = findViewById(R.id.main_tool_rbt);
        mainMeRbt = findViewById(R.id.main_me_rbt);
        mainListRgp = findViewById(R.id.main_list_rgp);
        drawerLayout = findViewById(R.id.drawer_layout);
    }


    private void openSidebar() {
        drawerLayout.openDrawer(Gravity.LEFT);
    }


    public void initFragment() {
        indexTabFragment = ShareUtils.getInstance().getInt("indexTabFragment", 1);
//        AppBaseFragment leftFragment =
//                (AppBaseFragment) RouterUtils.getNavigate(ArouterPath.NINE_LEFT_FRAGMENT);
//        setReplacementPage(R.id.fragment_left_fly, leftFragment);


        AppBaseFragment standardSbFragment =
                (AppBaseFragment) RouterUtils.getNavigate(ArouterPath.SB_SB_FRAGMENT);

        AppBaseFragment transFragment =
                (AppBaseFragment) RouterUtils.getNavigate(ArouterPath.TRANS_TRANS_FRAGMENT);
        AppBaseFragment chatFragment =
                (AppBaseFragment) RouterUtils.getNavigate(ArouterPath.CHAT_CHAT_FRAGMENT);

        mainListRgp.setOnCheckedChangeListener((radioGroup, i) -> {
            switch (i) {
//                case R.id.main_home_rbt:
//                    setIndexTabFragment(0);
//                    setMainReplacementPage(standardSbFragment);
//                    break;
                case R.id.main_input_rbt:
                    setIndexTabFragment(1);
                    setMainReplacementPage(transFragment);
                    break;
//                case R.id.main_search_rbt:
//                    setIndexTabFragment(2);
//                    setMainReplacementPage(chatFragment);
//                    break;
              /*    case R.id.main_tool_rbt:
                    setTabsBg(3);
                    setReplacementPage(toolsFragment);
                    break;
                case R.id.main_me_rbt:
                    setTabsBg(4);
                    setReplacementPage(meFragment);
                    break;*/
                default:
                    break;
            }
        });
        setTabsBg(indexTabFragment);
    }

    int indexTabFragment = 0;

    public void setTabsBg(int bg_status) {
        setIndexTabFragment(bg_status);
        setFragment();
    }

    public void setFragment() {
        RadioButton radioButton = findViewById(mainListRgp.getChildAt(indexTabFragment).getId());
//        radioButton.setChecked(true);
        mainListRgp.check(radioButton.getId());
    }

    public void setMainReplacementPage(AppBaseFragment appBaseFragment) {
        setReplacementPage(R.id.fragment_container_fly, appBaseFragment);
    }

    @Override
    public void onEventBus(BusEvent event) {
        super.onEventBus(event);
        if ("打开侧边栏".equals(event.getAction())) {
            openSidebar();
        }
    }
}
